import type { DressStatus, ArtistStatus, OrderStatus, ServiceItem } from '#/api/wedding';
import dayjs from 'dayjs';
import isBetween from 'dayjs/plugin/isBetween';

dayjs.extend(isBetween);

/**
 * 格式化婚纱状态
 */
export function formatDressStatus(status: DressStatus): string {
  const statusMap: Record<DressStatus, string> = {
    AVAILABLE: '可用',
    RESERVED: '已预约',
    RENTED: '已租出',
    MAINTENANCE: '维护中',
    RETIRED: '已下架',
  };
  return statusMap[status] || status;
}

/**
 * 格式化服务状态
 */
export function formatServiceStatus(status: ArtistStatus): string {
  const statusMap: Record<ArtistStatus, string> = {
    AVAILABLE: '可预约',
    BUSY: '服务中',
    BOOKED: '已预约',
    OFF_DUTY: '休息中',
  };
  return statusMap[status] || status;
}

/**
 * 格式化订单状态
 */
export function formatOrderStatus(status: OrderStatus): string {
  const statusMap: Record<OrderStatus, string> = {
    DRAFT: '草稿',
    CONFIRMED: '已确认',
    PAID: '已支付',
    IN_PROGRESS: '进行中',
    COMPLETED: '已完成',
    CANCELLED: '已取消',
  };
  return statusMap[status] || status;
}

/**
 * 计算订单总金额
 */
export function calculateTotalAmount(serviceItems: ServiceItem[]): {
  subtotal: number;
  discountAmount: number;
  totalAmount: number;
} {
  const subtotal = serviceItems.reduce((sum, item) => sum + (item.price * item.quantity), 0);
  const discountAmount = 0; // 可以根据业务规则计算优惠
  const totalAmount = subtotal - discountAmount;
  
  return {
    subtotal,
    discountAmount,
    totalAmount,
  };
}

/**
 * 检查档期冲突
 */
export function checkScheduleConflict(
  targetDate: string,
  bookedDates: Array<{ startDate: string; endDate: string }>
): boolean {
  const target = dayjs(targetDate);
  
  return bookedDates.some(({ startDate, endDate }) => {
    return target.isBetween(startDate, endDate, 'day', '[]');
  });
}

/**
 * 格式化日期
 */
export function formatDate(date: string | Date, format: string = 'YYYY-MM-DD'): string {
  return dayjs(date).format(format);
}

/**
 * 格式化日期时间
 */
export function formatDateTime(date: string | Date): string {
  return dayjs(date).format('YYYY-MM-DD HH:mm:ss');
}

/**
 * 计算日期差（天数）
 */
export function getDateDiff(startDate: string, endDate: string): number {
  return dayjs(endDate).diff(dayjs(startDate), 'day');
}

/**
 * 生成订单号
 */
export function generateOrderId(): string {
  const timestamp = Date.now();
  const random = Math.floor(Math.random() * 10000).toString().padStart(4, '0');
  return `WO${timestamp}${random}`;
}

/**
 * 格式化价格
 */
export function formatPrice(price: number, showSymbol: boolean = true): string {
  const formatted = price.toFixed(2);
  return showSymbol ? `¥${formatted}` : formatted;
}

/**
 * 验证手机号
 */
export function validatePhone(phone: string): boolean {
  const phoneRegex = /^1[3-9]\d{9}$/;
  return phoneRegex.test(phone);
}

/**
 * 验证邮箱
 */
export function validateEmail(email: string): boolean {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
  return emailRegex.test(email);
}

/**
 * 获取服务类型名称
 */
export function getServiceTypeName(type: string): string {
  const typeMap: Record<string, string> = {
    DRESS: '婚纱',
    MAKEUP: '化妆',
    PHOTOGRAPHY: '摄影',
    PACKAGE: '套餐',
  };
  return typeMap[type] || type;
}

/**
 * 获取服务等级名称
 */
export function getServiceLevelName(level: string): string {
  const levelMap: Record<string, string> = {
    JUNIOR: '初级',
    SENIOR: '高级',
    MASTER: '首席',
    CHIEF: '总监',
  };
  return levelMap[level] || level;
}

/**
 * 计算评分星级
 */
export function getRatingStars(rating: number): number {
  return Math.round(rating * 2) / 2; // 四舍五入到0.5
}

/**
 * 获取剩余天数
 */
export function getRemainingDays(targetDate: string): number {
  const now = dayjs();
  const target = dayjs(targetDate);
  return target.diff(now, 'day');
}

/**
 * 判断是否即将到期（7天内）
 */
export function isExpiringSoon(targetDate: string, days: number = 7): boolean {
  const remaining = getRemainingDays(targetDate);
  return remaining >= 0 && remaining <= days;
}

